package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.OfferForm;

@Component
public class OfferFormValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return OfferForm.class.equals(clazz);
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		OfferForm form = (OfferForm) target;
		if(form.getOfferPrice()<=0){
			errors.rejectValue("offerPrice", "negativeorzero");
		}else if(form.getOfferPrice()<form.getBasePrice()){
			errors.rejectValue("offerPrice", "lessthanbaseprice");
		}
		if(form.getCount()<=0){
			errors.rejectValue("count", "negativeorzero");
		}
	}
}